clear all
use Ma_EDCC_2020_Data.dta

********************************************************************************
** Tab 1
********************************************************************************

** Contract land
sum hh_ownld hh_ownplt if pers_id==1 & yr==2016 & county_id==1
sum hh_ownld hh_ownplt if pers_id==1 & yr==2016 & county_id==2
sum hh_ownld hh_ownplt if pers_id==1 & yr==2016 & county_id==3
sum hh_ownld hh_ownplt if pers_id==1 & yr==2016 & county_id==4
sum hh_ownld hh_ownplt if pers_id==1 & yr==2016 

** Land use
** Definition of f1-f16 is found in "variable definition"
** f1-f16 represent different types of land arrangements
gen ss=0
replace ss=1 if (f2==1|f9==1|f15==1|f16==1) 

gen pt=0
replace pt=1 if (f3==1|f4==1|f5==1|f6==1|f7==1|f8==1|f12==1)& f15!=1 & f16!=1

gen yl=0
replace yl=1 if (f1==1|f10==1|f11==1|f14==1) 

** Land arrangements
sum ss pt yl f13 hh_abd if pers_id==1 & yr==2016 & county_id==1 & hh_elcontr!=0
sum ss pt yl f13 hh_abd if pers_id==1 & yr==2016 & county_id==2 & hh_elcontr!=0
sum ss pt yl f13 hh_abd if pers_id==1 & yr==2016 & county_id==3 & hh_elcontr!=0
sum ss pt yl f13 hh_abd if pers_id==1 & yr==2016 & county_id==4 & hh_elcontr!=0
sum ss pt yl f13 hh_abd* if pers_id==1 & yr==2016 & hh_elcontr!=0

** Households leasing in or out
gen hh_rent = 0
replace hh_rent =1 if hh_rin ==1
replace hh_rent =1 if hh_rout==1
sum hh_rent if pers_id==1 & yr==2016 & hh_elcontr!=0


********************************************************************************
** Table 2
** HH Mlogit: exit mode
********************************************************************************

** hh_lf: dummy variable which equals one if a hh is a large farm

local hh_v "hh_lc hh_ldreall hh_ldexpp hh_elcontr hh_subs"
local hh_v2 "hh_lb genr hh_ownplotsz hh_paddprop_contr hh_depd hh_hsld hh_hsch hh_msch hh_youngm hh_youngw hh_vilhsv hh_pns"
local vil_v "vil_tocnt vil_tocd vil_skwg_peakm vil_lfel_perc vil_unskwg_peakm vil_tilprop vil_trpprop vil_hvprop"
local vil_v2 "vil_sub vil_area vil_pop vil_hkhh vil_hhoutprop vil_fld vil_fld_hill vil_fld_padd vil_firm vil_firmemply vil_oldprop vil_mlprop"
local fe "yr16" 

qui mlogit hh_ex ///
`hh_v' `hh_v2' `vil_v' `vil_v2' `fe' ///
if pers_id==1 & hh_lf==0, baseoutcome(0) vce(cluster vil_id)
est sto m, title(mlogit)

esttab m, keep (hh_lc hh_ldreall hh_ldexpp vil_tocnt hh_elcontr hh_subs) ///
cells(b(star fmt(2)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) ///
legend label varlabels(_cons Constant) ///
stats(N r2 df_r)

******************************
** HH Ologit: exit mode
******************************

** hh_lf: dummy variable which equals one if a hh is a large farm

local hh_v "hh_lc hh_ldreall hh_ldexpp hh_elcontr hh_subs"
local hh_v2 "hh_lb genr hh_ownplotsz hh_paddprop_contr hh_depd hh_hsld hh_hsch hh_msch hh_youngm hh_youngw hh_vilhsv hh_pns"
local vil_v "vil_tocnt vil_tocd vil_skwg_peakm vil_lfel_perc vil_unskwg_peakm vil_tilprop vil_trpprop vil_hvprop"
local vil_v2 "vil_sub vil_area vil_pop vil_hkhh vil_hhoutprop vil_fld vil_fld_hill vil_fld_padd vil_firm vil_firmemply vil_oldprop vil_mlprop"
local fe "yr16" 

qui ologit hh_ex ///
`hh_v' `hh_v2' `vil_v' `vil_v2' `fe' ///
if pers_id==1 & hh_lf==0, vce(cluster vil_id)
est sto m, title(ologit)

esttab m, keep (hh_lc hh_ldreall hh_ldexpp vil_tocnt hh_elcontr hh_subs) ///
cells(b(star fmt(2)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) ///
legend label varlabels(_cons Constant) ///
stats(N r2 df_r)

******************************************
** HH Tobit: yearly self-cultivation size
******************************************

** Average effects
local hh_v "hh_lc hh_ldreall hh_ldexpp hh_elcontr hh_subs"
local hh_v2 "hh_lb genr hh_ownplotsz hh_paddprop_contr hh_depd hh_hsld hh_hsch hh_msch hh_youngm hh_youngw hh_vilhsv hh_pns"
local vil_v "vil_tocnt vil_tocd vil_skwg_peakm vil_lfel_perc vil_unskwg_peakm vil_tilprop vil_trpprop vil_hvprop"
local vil_v2 "vil_sub vil_area vil_pop vil_hkhh vil_hhoutprop vil_fld vil_fld_hill vil_fld_padd vil_firm vil_firmemply vil_oldprop vil_mlprop"
local fe "yr16" 

qui tobit hh_el ///
`hh_v' `hh_v2' `vil_v' `vil_v2' `fe' ///
if pers_id==1 & hh_lf==0, ll(0) vce(cluster vil_id)

margins, dydx(`hh_v' vil_tocnt) predict(ystar(0,.)) post

** Average effects, interior solutions (w/ zero)
local hh_v "hh_lc hh_ldreall hh_ldexpp hh_elcontr hh_subs"
local hh_v2 "hh_lb genr hh_ownplotsz hh_paddprop_contr hh_depd hh_hsld hh_hsch hh_msch hh_youngm hh_youngw hh_vilhsv hh_pns"
local vil_v "vil_tocnt vil_tocd vil_skwg_peakm vil_lfel_perc vil_unskwg_peakm vil_tilprop vil_trpprop vil_hvprop"
local vil_v2 "vil_sub vil_area vil_pop vil_hkhh vil_hhoutprop vil_fld vil_fld_hill vil_fld_padd vil_firm vil_firmemply vil_oldprop vil_mlprop"
local fe "yr16" 

qui reg hh_el ///
`hh_v' `hh_v2' `vil_v' `vil_v2' `fe' ///
if pers_id==1 & hh_lf==0,  vce(cluster vil_id)
est sto m, title(ols)

esttab m, keep (hh_lc hh_ldreall hh_ldexpp vil_tocnt hh_elcontr hh_subs) ///
cells(b(star fmt(2)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) ///
legend label varlabels(_cons Constant) ///
stats(N r2 df_r) 

** Average effects, interior solutions (no zero)
local hh_v "hh_lc hh_ldreall hh_ldexpp hh_elcontr hh_subs"
local hh_v2 "hh_lb genr hh_ownplotsz hh_paddprop_contr hh_depd hh_hsld hh_hsch hh_msch hh_youngm hh_youngw hh_vilhsv hh_pns"
local vil_v "vil_tocnt vil_tocd vil_skwg_peakm vil_lfel_perc vil_unskwg_peakm vil_tilprop vil_trpprop vil_hvprop"
local vil_v2 "vil_sub vil_area vil_pop vil_hkhh vil_hhoutprop vil_fld vil_fld_hill vil_fld_padd vil_firm vil_firmemply vil_oldprop vil_mlprop"
local fe "yr16" 

qui reg hh_el ///
`hh_v' `hh_v2' `vil_v' `vil_v2' `fe' ///
if pers_id==1 & hh_lf==0 & hh_el!=0,  vce(cluster vil_id)
est sto m, title(ols w/o zero)

esttab m, keep (hh_lc hh_ldreall hh_ldexpp vil_tocnt hh_elcontr hh_subs) ///
cells(b(star fmt(2)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) ///
legend label varlabels(_cons Constant) ///
stats(N r2 df_r) 

********************************************************************************
** Table A2
********************************************************************************

** household and vil variables
local hh_v "hh_lc hh_ldreall hh_ldexpp hh_elcontr hh_subs"
local hh_v2 "hh_lb genr hh_ownplotsz hh_paddprop_contr hh_depd hh_hsld hh_hsch hh_msch hh_youngm hh_youngw hh_vilhsv hh_pns"
local vil_v "vil_tocnt vil_tocd vil_skwg_peakm vil_lfel_perc vil_unskwg_peakm vil_tilprop vil_trpprop vil_hvprop"
local vil_v2 "vil_sub vil_area vil_pop vil_hkhh vil_hhoutprop vil_fld vil_fld_hill vil_fld_padd vil_firm vil_firmemply vil_oldprop vil_mlprop"

sum `hh_v' `hh_v2' `vil_v' `vil_v2' if pers_id==1 & hh_lf==0 & hh_elcontr!=0

** Total home prod costs: croping costs, machinery costs, labor costs, and livestock costs
gen hh_hpc = hh_cropc + hh_mechc + hh_hirelbc + hh_lsc  

** Chnage unit of value
qui foreach i in hh_hpv hh_hpc hh_lsv{
	replace `i'=`i'*1000
	}
	
** Define large ranches, the top 2.5%
gen hh_lrch=0
replace hh_lrch=1 if hh_lsv>100000

** Total home production days
gen hh_hpd = 0
qui foreach i of numlist 1/512{
	egen temp1=sum(hp_d) if hhno==`i' & yr==2015 
	egen temp2=sum(hp_d) if hhno==`i' & yr==2016 
	egen temp3=max(temp1) if hhno==`i' & yr==2015 
	egen temp4=max(temp2) if hhno==`i' & yr==2016 
	
	replace hh_hpd=temp3 if hhno==`i' & yr==2015 
	replace hh_hpd=temp4 if hhno==`i' & yr==2016
	drop temp*
	}

** exclude households that do not produce on farm or raise livestock
sum hh_hpv hh_hpd hh_hpc if pers_id==1 & hh_lf==0 & hh_lrch==0 & hh_hpd!=0

